//
//  NewFeatureViewController.swift
//  sinaweibo
//
//  Created by mac on 15/11/12.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

private let imageCount = 4
class NewFeatureViewController: UICollectionViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFeatureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)


    }

    init() {
    
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureViewCell
    
        cell.index = indexPath.item
        
        return cell
    }
    
//    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
//        
//        let page = Int(scrollView.contentOffset.x / screenW)
//        if page == imageCount - 1 {
//            if let cell = collectionView?.visibleCells().last as? NewFeatureViewCell {
//            
//                cell.startAnimation()
//            }
//        }
//    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let path = collectionView.indexPathsForVisibleItems().last! as NSIndexPath
        let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureViewCell
        
        if path.item == imageCount - 1 {
        
            cell.startAnimation()
        }
    }
}

class NewFeatureViewCell:UICollectionViewCell{

    var index: Int = 0 {
        didSet{
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
            if index == imageCount - 1 {
                startButton.hidden = false
            }else{
                startButton.hidden = true
            }
        }
    }
    //开始体验按钮的点击事件
    @objc private func start() {
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func startAnimation() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
                print("飒")
        }
    }
    
    private func setupUI() {
    
        contentView.addSubview(iconImage)
        contentView.addSubview(startButton)
        
        iconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.snp_edges)
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
            make.centerX.equalTo(contentView.snp_centerX)
        }
        
        startButton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
//        startButton.hidden = true
    }
    
    private lazy var iconImage:UIImageView = UIImageView()
//    UIButton(title: "开始体验", imageName: "new_feature_finish_button", color: nil)
    private lazy var startButton:UIButton = UIButton(title: "开始体验", backImage:"new_feature_finish_button", color: nil)
}

